package com.sky.service.common.impl;

import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.service.common.CommonDishService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service("commonDishService")
@Slf4j
public class CommonCommonDishServiceImpl implements CommonDishService {

    private final DishMapper dishMapper;
    private final DishFlavorMapper dishFlavorMapper;

    @Autowired
    public CommonCommonDishServiceImpl(DishMapper dishMapper, DishFlavorMapper dishFlavorMapper) {
        this.dishMapper = dishMapper;
        this.dishFlavorMapper = dishFlavorMapper;
    }

    /**
     * 根据分类id查询菜品
     *
     * @param dish 菜品实体类
     * @return 菜品实体类VO
     */
    @Override
    public List<DishVO> listDishWithFlavor(Dish dish) {
        log.info("执行 根据分类id查询菜品 业务: {}",dish);
        List<Dish> dishList = dishMapper.listDish(dish);

        List<DishVO> dishVOList = new ArrayList<>();

        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d,dishVO);

            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishFlavorMapper.getFlavorByDishId(d.getId());

            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }

        return dishVOList;
    }
}
